package binarysearch;

public class BasicBinarySearch {

    public static void main(String[] args) {
        int[] nums = {1,4,7,9,10,14,20,56,89};
        System.out.println(new BasicBinarySearch().getIndex(nums, 4));
    }

    public int getIndex(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        int start = 0;
        int end = nums.length - 1;
        int mid;
        while (start + 1 < end) {
            mid = (start + end) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                // 把 end 移到 mid 的下标位置
                end = mid;
            } else {
                // 把 start 移到 mid 的下标位置
                start = mid;
            }
            System.out.println("=======");
        }
        if (nums[start] == target) {
            System.out.println("=====start=====");
            return start;
        }
        if (nums[end] == target) {
            System.out.println("=====end=====");
            return end;
        }
        return -1;
    }

}
